package com.traffic.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.traffic.annotation.ApiJsonObject;
import com.traffic.annotation.ApiJsonProperty;
import com.traffic.constant.Status;
import com.traffic.entity.Collect;

import com.traffic.entity.Resource;
import com.traffic.exception.BaseException;
import com.traffic.model.ApiResponse;

import com.traffic.service.BaidumapService;
import com.traffic.service.CollectionService;
import com.traffic.service.ResourceService;
import com.traffic.util.BaidumapUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = "地点收藏相关接口")
@RestController
@RequestMapping(value = "/collection")
@Slf4j
public class CollectionController {

    @Autowired
    CollectionService collectionService;

    @Autowired
    BaidumapUtil baidumapUtil;

    @Autowired
    BaidumapService baidumapService;

    @Autowired
    ResourceService resourceService;

    @ApiOperation(value = "【查看收藏地点】", notes = "全部地点")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "userid", value = "用户", required = true)
    @GetMapping("/all/{userid}")
    public ApiResponse AllCollection(@PathVariable Integer userid) {

        List<Collect> cols = collectionService.queryAllCollections(userid);

        return ApiResponse.ofStatus(Status.OK, cols);

    }

    @ApiOperation(value = "【增加收藏地点】", notes = "新增地点")
    @PostMapping("/insert")
    @ApiJsonObject(name = "collect", value = {
            @ApiJsonProperty(type = Integer.class, key = "user_id", example = "1", description = "用户id"),
            @ApiJsonProperty(type = Double.class, key = "longitude", example = "118.23456", description = "经度"),
            @ApiJsonProperty(type = Double.class, key = "latitude", example = "30.34567", description = "纬度"),
            @ApiJsonProperty(type = String.class, key = "icon", example = "http://188.131.136.54:9000/traffic/20201029/上报.png", description = "图标地址"),
    })
    public ApiResponse insertCollection(@RequestBody Map<String, Object> collect) {

        String param[] = new String[]{"user_id", "longitude", "latitude", "icon"};

        List<String> collList = CollUtil.newArrayList(param);
        if (!collList.containsAll(CollUtil.newArrayList(collect.keySet().toArray()))) {
            throw new BaseException(Status.PARAM_ERROR);
        }

        Collect col = new Collect();
        BeanUtil.copyProperties(collect, col, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));

        String ak = baidumapUtil.getAk();

        Map<String,Object> params = new HashMap<String,Object>();

        params.put("location", collect.get("latitude").toString()+","+collect.get("longitude").toString());
        params.put("ak", ak);


        String returnaddress = baidumapService.getAddress(params);
        JSONObject json = JSON.parseObject(returnaddress).getJSONObject("result");

        JSONArray pois = json.getJSONArray("pois");
        JSONObject poi = pois.getJSONObject(0);

        String detail = poi.getString("addr");
        String address = poi.getString("name");
        col.setAddress(address);
        col.setDetail(detail);

        collectionService.save(col);

        return ApiResponse.ofStatus(Status.OK);
    }


    @ApiOperation(value = "【删除收藏地点】", notes = "删除地点")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "deleteId", value = "删除地点的id", required = true)
    @DeleteMapping(value = "/delete/{deleteId}")
    public ApiResponse deleteCollection(@PathVariable Integer deleteId) {

        collectionService.removeById(new Integer(deleteId));

        return ApiResponse.ofStatus(Status.OK);
    }

}